﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StandardManager.DBItems
{
    public class BookContents
    {
        public string BookCode;
        public int PageIndex;
        public string BookContent;
        public byte[] BookImg;
        public string ContentChapter;

        public override string ToString()
        {
            return BookCode+"/"+PageIndex+(string.IsNullOrEmpty(ContentChapter)==false?"/"+ ContentChapter:"");
        }

        internal static BookContents FromDataRowOfPageIndex(DataRow dataRow)
        {
            BookContents books = new BookContents()
            {
                BookCode = dataRow["BookCode"].ToString(),
                PageIndex = int.Parse(dataRow["PageIndex"].ToString()),
                ContentChapter = dataRow["ContentChapter"]?.ToString()
            };
            return books;
        }
        internal static BookContents FromDataRow(DataRow dataRow)
        {
            BookContents books = new BookContents()
            {
                BookCode = dataRow["BookCode"].ToString(),
                PageIndex = int.Parse(dataRow["PageIndex"].ToString()),
                BookContent= dataRow["BookContent"].ToString(),
                BookImg = (byte[])dataRow["BookImg"],
                ContentChapter= dataRow["ContentChapter"]?.ToString(),
            };
            return books;
        }

        internal static string GetRecord(string code, int? selIndex)
        {
            return "select * from BookContents where bookCode='" + code + "' and PageIndex=" + selIndex;
        }

        internal static string GetRecordsCountSql(string code)
        {
            return "select PageIndex,BookCode,ContentChapter from BookContents where bookCode='" + code + "' order by PageIndex";
        }

        internal string BuildInsertSql()
        {
            var str = "insert OR IGNORE into BookContents(BookCode,PageIndex,BookContent,ContentChapter,BookImg) values('{0}','{1}','{2}','{3}',@BookImg)";
            return String.Format(str, BookCode, PageIndex,  Regex.Replace(BookContent,"'","\""), ContentChapter);
        }

        internal static string SearchContent(string text, List<string> filterItems)
        {
            var str = "select * from BookContents where REPLACE(REPLACE(bookContent, '\r', ''), '\n', '') like '%" + text + "%'";
            if (filterItems.Count > 0)
            {
                str += " and BookCode in(";
                for (var i = 0; i < filterItems.Count; i++)
                {
                    if (i != 0)
                    {
                        str += ",";
                    }
                    str += "'" + filterItems[i] + "'";
                }
                str += ")";
            }
            str += " order by bookCode,pageIndex";
            return str;
        }

        #region 更新接口
        internal string BuildUpdateContentSql()
        {
            return string.Format("update BookContents set bookContent='{0}' where BookCode='{1}' and pageIndex={2}",
                Regex.Replace(BookContent, "\r\n", "\n"), BookCode, PageIndex);
        }
        internal string BuildUpdateMetadataSql()
        {
            return string.Format("update BookContents set ContentChapter='{0}' where BookCode='{1}' and pageIndex={2}",
                ContentChapter, BookCode, PageIndex);
        }
        #endregion
    }
}
